$(document).ready(function(){
    
});
/******************************************************
 * Address part
 ******************************************************/
function add_address() {
    var consignee = $("#id_consignee").val();
    var street_address = $("#id_street_address").val();
    var postal_code = $("#id_postal_code").val();
    var city = $("#id_city").val();
    if ($("#id_state").val()!='') {
        var state = $("#id_state").val();
    }
    var country = $("#id_country").val();
    var phone = $("#id_phone").val();
    var submit = "add"
    if ($("#address_id").val()!='') {
        var address_id = $("#address_id").val();
        data = {
            'consignee':consignee,
            'street_address': street_address,
            'postal_code': postal_code,
            'city': city,
            'country': country,
            'phone': phone,
            'address_id': address_id,
            'submit': submit
        }
    }else {
        data = {
            'consignee':consignee,
            'street_address': street_address,
            'postal_code': postal_code,
            'city': city,
            'country': country,
            'phone': phone,
            'submit': submit
        }
    }
    url = "/user_panel/account_management/delivery_address/"
    $.post(url, data, function(result){
        if (result['success']==true) {
            document.getElementById("delivery-address-form").reset();
            $("#address_id").val('');
            if(document.getElementById("my-address-"+data['address_id'])==undefined){
                if (document.getElementById("empty-address")!=undefined){
                    $("#empty-address").remove();
                }
                if (state!=undefined) {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+state+", "+country+", "+phone;
                }
                else {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+country+", "+phone;
                }
                $("#my-delivery-address p:last").after('<p id="my-address-'+result['address_id']+'">'
                            +'"'+address_string+'"'
                            +'<br>'
                            +'<input type="button" value="Modify" onclick="modify_address('+result['address_id']+')" />'
                            +'<input type="button" value="Delete" onclick="delete_address('+result['address_id']+')" />'
                            +'</p>');
            }
            else {
                if (state!=undefined) {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+state+", "+country+", "+phone;
                }
                else {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+country+", "+phone;
                }
                $("#my-address-"+data['address_id']).html('"'+address_string+'"'
                            +'<br>'
                            +'<input type="button" value="Modify" onclick="modify_address('+result['address_id']+')" />'
                            +'<input type="button" value="Delete" onclick="delete_address('+result['address_id']+')" />'
                            +'</p>');
            }
        }
        else if (result['success']==false) {
            alert("The address has already been saved!");
        }
    });
}

function modify_address(address_id) {
    var submit = "modify"
    url = "/user_panel/account_management/delivery_address/"
    $.post(url, {'address_id':address_id, 'submit':submit}, function(data){
        if (data['success']==true) {
            $("#address_id").val(data['address_id']);
            $("#id_consignee").val(data['consignee']);
            $("#id_street_address").val(data['street_address']);
            $("#id_postal_code").val(data['postal_code']);
            $("#id_city").val(data['city']);
            $("#id_state").val(data['state']);
            $("#id_country").val(data['country']);
            $("#id_phone").val(data['phone']);
        }
    });
}

function delete_address(address_id) {
    var submit = "delete"
    url = "/user_panel/account_management/delivery_address/"
    $.post(url, {'address_id':address_id, 'submit':submit}, function(data){
        if (data['success']==true) {
            $("#my-address-"+address_id).remove();
            document.getElementById("delivery-address-form").reset();
            $("#address_id").val('');
            if(data['address_amount']==0) {
                $("#my-delivery-address p:last").after('<p id="empty-address"><b>You do not have any addresses!</b></p>');
            }
        }
    });
}
/********End for Address part******************************************/

/****************************************
 * action for comment and rate page
 ****************************************/
function rate_change_value(rate_id,value){
    var rate_block_id = 'rate-block-'+rate_id;
    // change the start image
    $('#'+rate_block_id).css("background-image","url('/site_media/images/rate-"+value+".jpg')");
    // change the value display    
    $('#'+rate_block_id+"-value").text(value);
    // change the hidden value
    $('#'+rate_block_id+"-value-hidden").val(value);
}
/*******End for comment and rate page************************************/